package com.yetm.service.impl;

import com.yetm.mapper.AccountMapper;
import com.yetm.domain.Account;
import com.yetm.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class AccountServiveImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    //原始方式
    public List<Account> getAllAccountBak() {
        System.out.println("service");

        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        List<Account> allAccount = mapper.getAllAccount();
        sqlSession.commit();
        sqlSession.close();

        return allAccount;
    }


    @Override
    //mybatis整合spring
    public List<Account> getAllAccount() {
        List<Account> allAccount = accountMapper.getAllAccount();

        return allAccount;
    }
}
